<?php

class Quantri_Model_Hanghoadv extends Zend_Db_Table_Abstract
{
    protected $_name="dmhanghoadv";
    protected $_primary="Id";
    
    var $Id, $Ten,$Idnhomhhdv,$Iddonvitinh,$GiaMua,$GiaBan,$MaVach,$DuongDanAnh,$TrangThai,$NgayTao;
    
    public function __construct() {
    	$this->_name = Zend_Registry::get('tablePrefix') . $this->_name;
    	    
    	parent::__construct();
    }
    
	
	public function GetAll() {
	    
	    $query = $this->_db->select()
	    						->from(array('T1'=>'os_dmhanghoadv'))
	    						->joinLeft(array('T2' => 'os_dmdonvitinh'), 'T1.Iddonvitinh = T2.Id', array('T2.Ten as DVT'))
	    						->joinLeft(array('T3' => 'os_dmnhomhhdv'), 'T1.Idnhomhhdv = T3.Id', array('T3.Ten as NhomHHDV'));
	    
	    
	    
	    
	    $stmt = $this->_db->query($query);
	    
	   	return $result = $stmt->fetchAll();
	    
	    var_dump($result);
	    
        //$data = $this->fetchall();
       // return $data;
    }
    
    public function Save()
    {
        $data = array(
        	'Ten' 	=> $this->Ten,
        	'Idnhomhhdv' 	=> $this->Idnhomhhdv,
        	'Iddonvitinh' => $this->Iddonvitinh,
        	'GiaMua' 	=> $this->GiaMua,
        	'GiaBan' => $this->GiaBan,
        	'MaVach' 	=> $this->MaVach,
        	'DuongDanAnh' => $this->DuongDanAnh,
            'TrangThai'=>$this->TrangThai,
            'NgayTao'=> date("Y-m-d")
        );
        
        if($this->Id=="-1")
        	$this->insert($data);
       	else
       	    $this->update($data, "Id = '$this->Id'");
    }
    
    public function Xoa($id)
    {
        $this->delete("id='". $id . "'");
    }
    
    public function GetById()
    {
        return $this->fetchAll("Id='$this->Id'", "", 1)->toArray();
    }
    
    
    
    public function UpdateTrangthai()
    {
        $data = array(
        	'Trangthai' => $this->Trangthai
        );
        
        $this->update($data, "Id = '$this->Id'");
    }
	
}

